package pos;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import pos.bigram.Training;


public class Baseline {

	private String mostUsedTag;
	private HashMap<String, String> baselineMap;

	public Baseline(HashMap<String, String> wordMap, String mostUsedTag) {
		this.baselineMap = wordMap;
		this.mostUsedTag = mostUsedTag;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String trainFile = "D:/study/natural language processing/Assignments/Sequential tagging/POS data/pos_files/train.pos";
		Training training = new Training();
		training.train(trainFile);
		HashMap<String, String> baselineMap = training.getBaselineMap();
		System.out.println(baselineMap);
		String mostUsedTag = training.getMostUsedTag();
		System.out.println(mostUsedTag);
		
		File testFile = new File("D:/study/natural language processing/Assignments/Sequential tagging/POS data/pos_files/test-obs.pos");
		
		File outputFile = new File("D:/study/natural language processing/Assignments/Sequential tagging/POS data/baseline-output.pos");
		
		Baseline test = new Baseline(baselineMap, mostUsedTag);
		test.test(testFile, outputFile);
	}

	private void test(File testFile, File outputFile) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
			BufferedReader reader = new BufferedReader(new FileReader(testFile));
			String line;
			while((line = reader.readLine()) != null) {
				String word = line.trim();
				String tag = baselineMap.get(word);
				if(tag!=null)
					writer.write(tag + " " + word + "\n");
				else 
					writer.write(mostUsedTag + " " + word + "\n");
			}
			writer.flush();
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
